% file estima_fcastroll_rmse.m
clear all; close all; clc; 
% cd('O:\PROJ_LIB\LJM\exog\chimod\stama\infdet_00q1_09q4\roll 06 03 10')
cd('O:\PROJ_LIB\LJM\exog\chimod\stama\infdet_par_00q1_09q4\roll 06 03 10')
load fcastroll_2000q1_2009q4; 
cd(cucd); 

nforc = 16; 
allforc_dsge = zeros(1+nforc, nz, nroll); 
allferr_dsge = zeros(1+nforc, nz, nroll); 

allforc_var1 = zeros(1+nforc, nz, nroll); 
allferr_var1 = zeros(1+nforc, nz, nroll); 

allforc_var2 = zeros(1+nforc, nz, nroll); 
allferr_var2 = zeros(1+nforc, nz, nroll); 

%% Generating Forecasts and Compute Forecast Errors
for ii = 1:nroll;
    
   dest = firstdest + 0.25*(ii - 1);
   dest_str = sample2date(dest, '');
   dest_str = dest_str{:}; 
   disp('')
   disp('____________________________________')
   disp(['forecasting at date ', dest_str ]); 
    
    nper_est = find(dest == sample);
    Y = Yfull(1:nper_est, :);
    
    if 17 >= nper_est;
        error('not enough periods');
    end
    trainvec = [17, nper_est];
    
    parvecmode = parevolve_mat(:, ii); 
    
    outforc=forc_dsge(Yfull(1:min(trainvec(2)+nforc, length(Yfull)), :),trainvec,funcmod,parvecmode,solveopt,addsol,nforc);  
    
    allforc_dsge(:, :, ii) = outforc.forc_dsge; 
    allferr_dsge(:, :, ii) = outforc.rmse_dsge; 
  
    allforc_var1(:, :, ii) = outforc.forc_var1; 
    allferr_var1(:, :, ii) = outforc.rmse_var1; 
    
    allforc_var2(:, :, ii) = outforc.forc_var2; 
    allferr_var2(:, :, ii) = outforc.rmse_var2; 
    
    
    cd(outpath); 
    eval(['save ', savefname, ' -append']); 
    cd(cucd); 
end
% sum of squared error
sse_dsge = sum(nan2nil(allferr_dsge).^2, 3); 
sse_var1 = sum(nan2nil(allferr_var1).^2, 3); 
sse_var2 = sum(nan2nil(allferr_var2).^2, 3);

sse_dsge = sse_dsge(2:end, :); 
sse_var1 = sse_var1(2:end, :); 
sse_var2 = sse_var2(2:end, :); 

Tvec = nroll*ones(nforc, 1) - (1:nforc)'; 
Tmat = repmat(Tvec, [1, nz]); 


rmse_dsge = (sse_dsge./Tmat).^0.5; 
rmse_var1 = (sse_var1./Tmat).^0.5; 
rmse_var2 = (sse_var2./Tmat).^0.5; 


rrmse_dsge_to_var2 = rmse_dsge./rmse_var2; 
rrmse_var1_to_var2 = rmse_var1./rmse_var2; 

cd(outpath); 
save fcastres rmse_dsge rmse_var1 rmse_var2 rrmse_dsge_to_var2 rrmse_var1_to_var2
cd(cucd); 


cd(outpath); 
eval(['save ', savefname, ' -append']); 
cd(cucd); 

%% Compute RMSE





